package com.lzx.iteam;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import com.lzx.iteam.adapter.MyTalkMessageAdapter;
import com.lzx.iteam.bean.MyTalkMessageData;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.GetMyTalkMessageMsg;
import com.lzx.iteam.net.LocalLogin;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.util.AllDialogUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * 人脉圈-我的消息
 * Created by Megan on 15/10/12.
 */
public class MyTalkMessageActivity extends Activity implements View.OnClickListener {


    private LinearLayout mLlBack,mLlNoData;
    private ListView mMyMessageList;
    private MyTalkMessageAdapter adapter;
    private ArrayList<MyTalkMessageData> mTalkMessageDatas;
    private CloudDBOperation mCloudDBOperation;
//    private PreferenceUtil mPreferenceUtil;
//    private String mUserId;
    private final int GET_MY_MESSAGE = 1000;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case GET_MY_MESSAGE:
                    if (msg.arg1 == 0) {
                      initData();
                    }
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_talk_message_layout);
        mCloudDBOperation= new CloudDBOperation(this);
//        mPreferenceUtil = PreferenceUtil.getInstance(this);
//        mUserId = mPreferenceUtil.getString(PreferenceUtil.CLIENT_USEID, "－1");
        mTalkMessageDatas=new ArrayList<MyTalkMessageData>();
        initView();
        initData();
        GetMyMessage();
    }


    private void initData(){
        new AsyncTask<String, Void, ArrayList<MyTalkMessageData>>(){
            @Override
            protected ArrayList<MyTalkMessageData> doInBackground(String... params) {
                return mCloudDBOperation.getTalkMyMsgData();
            }

            @Override
            protected void onPostExecute(ArrayList<MyTalkMessageData> talkMessageDatas) {
                super.onPostExecute(talkMessageDatas);
                if (mTalkMessageDatas != null) {
                    mTalkMessageDatas.clear();
                }
                mTalkMessageDatas.addAll(talkMessageDatas);
                if(mTalkMessageDatas!=null&&mTalkMessageDatas.size()>0){
                    mLlNoData.setVisibility(View.GONE);
                    mMyMessageList.setVisibility(View.VISIBLE);
                    if (adapter != null) {
                        adapter.bindData(mTalkMessageDatas);
                    } else {
                        adapter = new MyTalkMessageAdapter(MyTalkMessageActivity.this);
                        adapter.bindData(mTalkMessageDatas);
                        mMyMessageList.setAdapter(adapter);
                    }}else{
                    mLlNoData.setVisibility(View.VISIBLE);
                    mMyMessageList.setVisibility(View.GONE);
                }
            }
        }.execute();

    }

    private void initView() {
        mLlNoData=(LinearLayout)findViewById(R.id.ll_my_talk_message_no_data);
        mLlBack = (LinearLayout) findViewById(R.id.my_talk_message_back);
        mMyMessageList = (ListView) findViewById(R.id.lv_my_talk_message_list);
        mMyMessageList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MyTalkMessageActivity.this, ContactsTalkDetailActivity.class);
                intent.putExtra("talk_id",mTalkMessageDatas.get(i).talkId);
                startActivity(intent);

            }
        });
        mMyMessageList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
                MyTalkMessageData myTalkMessageData=(MyTalkMessageData)adapter.getItem(position);
                        showExitGroupDlg(myTalkMessageData);
                return false;
            }
        });
        mLlBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.my_talk_message_back:
                finish();
                break;
        }
    }


    /**
     *删除这条我的消息
     */
    public void showExitGroupDlg(final MyTalkMessageData myTalkMessageData) {
        AllDialogUtil dialogUtil = AllDialogUtil.getInstance(MyTalkMessageActivity.this);
        String okText = this.getString(R.string.label_btn_ok);
        String cancelText = this.getString(R.string.cancel);
            dialogUtil.titleDeleteMsgStyle(okText, cancelText, "删除此条消息");
        dialogUtil.setOnDialogListener(new AllDialogUtil.DialogOnClickListener() {
            @Override
            public void onConfirmClick(Dialog lDialog) {
                mCloudDBOperation.deleteOneTalkMyMsg(myTalkMessageData._id);
                lDialog.dismiss();
            }

            @Override
            public void onCancelClick(Dialog lDialog) {
                lDialog.dismiss();

            }

        });

    }


    private void GetMyMessage() {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
                .getInstance().mSid));
        GetMyTalkMessageMsg message = new GetMyTalkMessageMsg(mHandler.obtainMessage(GET_MY_MESSAGE), MyTalkMessageActivity.this);
        message.mApi = AsynHttpClient.API_TALK_MSG;
        message.mParams = nvps;
        AsynHttpClient.getInstance(this).execTalkHttp(message);
    }

}
